//
//  BIDEmailDetailViewController.m
//  JuanChengOA
//
//  Created by mal on 2017/7/28.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDEmailDetailViewController.h"
#import "BIDEmailReceiveConditionViewController.h"
#import "BIDMenuView.h"

//已发邮件详情
static NSString *detailURL = @"emailReplyAndroid!toReadInbox.action";

//删除邮件
static NSString *deleteURL = @"emailAndroid!dropSendedEmail.action";

@interface BIDEmailDetailViewController ()<BIDMenuViewDelegate, UIAlertViewDelegate>
{
    BIDMenuView *_menuView;
    BOOL _bShowMenu;
}
@end

@implementation BIDEmailDetailViewController
@synthesize emailID;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"详情";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_more.png"] style:UIBarButtonItemStylePlain target:self action:@selector(functionHandler)];
    [self loadData];
}

- (void)loadData
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], detailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"emailId=%@", emailID];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            NSDictionary *dic = responseDic[@"email"];
            NSMutableString *strExternalContent = [NSMutableString string];
            NSString *strTitle = dic[@"title"];
            NSString *strPublishInfo = [[NSString alloc] initWithFormat:@"%@ %@", dic[@"createName"], dic[@"time"]];
            if(strTitle.length > 0)
            {
                [strExternalContent appendFormat:@"<p style='margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: normal; color: rgb(0, 0, 0); font-size: 20px; line-height: normal; font-family: 微软雅黑; font-weight:900, widows: 1;'>%@</p>", strTitle];
            }
            if(strPublishInfo.length > 0)
            {
                [strExternalContent appendFormat:@"<p style='margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: normal; color: rgb(0, 0, 0); font-size: 11px; line-height: normal; font-family: 微软雅黑; widows: 1;'>%@</p><br/>", strPublishInfo];
            }
            //
            //NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[dic[@"content"] dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
            [strExternalContent appendString:dic[@"content"]];
            [_webView loadHTMLString:strExternalContent baseURL:nil];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  更多
 */
- (void)functionHandler
{
    //弹出菜单
    if(!_menuView)
    {
        NSArray *menuNameArr = @[@"接收情况", @"删除邮件"];
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        //menuWidth,menuHeight,triangleWidth,triangleHeight,triangleToRight
        BIDMenuAttribute menuAttribute = {120, 5, 10, 15, 14};
        int menuViewPosX = screenSize.width - 26 - (menuAttribute.menuWidth-menuAttribute.triangleToRight-menuAttribute.triangleWidth/2);
        int menuViewPosY = 2;
        _menuView = [[BIDMenuView alloc] initWithFrame:CGRectMake(menuViewPosX, menuViewPosY, menuAttribute.menuWidth, menuAttribute.menuHeight) menuNameArr:menuNameArr imgNameArr:nil menuAttribute:menuAttribute];
        _menuView.delegate = self;
    }
    if(!_bShowMenu)
    {
        _bShowMenu = YES;
        [self.view addSubview:_menuView];
    }
    else
    {
        _bShowMenu = NO;
        [UIView animateWithDuration:1.0f animations:^{
            //_menuView.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
        } completion:^(BOOL bFinished){
            if(bFinished)
            {
                [_menuView removeFromSuperview];
            }
        }];
    }
}

#pragma mark - BIDMenuViewDelegate
- (void)selectMenuAtIndex:(int)index
{
    _bShowMenu = NO;
    switch(index)
    {
        case 0:
        {
            //接收情况
            BIDEmailReceiveConditionViewController *conditionViewController = [[BIDEmailReceiveConditionViewController alloc] init];
            conditionViewController.emailId = emailID;
            [self.navigationController pushViewController:conditionViewController animated:YES];
        }
            break;
        case 1:
        {
            //删除
            [self deleteEmail];
        }
            break;
    }
}

- (void)deleteEmail
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], deleteURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"ids=%@", emailID];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            NSNumber *value = [responseDic objectForKey:@"isSuccess"];
            if([value intValue]==1)
            {
                [BIDGlobalMethods showAlertView:@"删除成功" buttonTitle:ALERT_BTN_TITLE delegate:self tag:1];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"删除失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}

#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag==1)
    {
        //删除成功
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
